﻿using ContaPlanner.Data;
using ContaPlanner.Models;
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;

namespace ContaPlanner
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            LanguageProperty.OverrideMetadata(
                typeof(FrameworkElement),
                new FrameworkPropertyMetadata(
                    XmlLanguage.GetLanguage(
                        CultureInfo.CurrentCulture.IetfLanguageTag
                    )
                )
            );
            using (var context = new ContaPlannerContext())
            {
                var cuentas = context.Cuentas
                    .Include("Banco")
                    .Include("Asociada");
                    //.OrderBy(c => c.Nombre);
                CuentasList.Cuentas = cuentas.ToList();
                var view = (CollectionView)CollectionViewSource.GetDefaultView(CuentasList.Cuentas);
                view.Filter = CuentasListFilter;
                var tipogroup = new PropertyGroupDescription("Categoria");
                if (view.GroupDescriptions != null) view.GroupDescriptions.Add(tipogroup);
            }
            CuentasList.SelectFirst();
        }

        private static bool CuentasListFilter(object item)
        {
            var cuenta = item as Cuenta;
            return cuenta != null && cuenta.Mostrar;
        }

        private void CuentasList_CuentaChanged(object sender, EventArgs e)
        {
            var cuenta = CuentasList.SelectedCuenta;
            if (cuenta == null) return;
            MovimientosList.Cuenta = cuenta;
            MovimientosList.SelectLast();
            //cuenta.GetMovimientosRecibos(DateTime.Now, DateTime.Now.AddDays(60));
        }

        private WindowState _lastState;
        private double _lastHeight, _lastWidth, _lastLeft, _lastTop;
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F11)
            {
                if (WindowStyle == WindowStyle.None)
                {
                    WindowStyle = WindowStyle.SingleBorderWindow;
                    WindowState = _lastState;
                    Width = _lastWidth;
                    Height = _lastHeight;
                    Left = _lastLeft;
                    Top = _lastTop;
                    Topmost = false;
                    ResizeMode = ResizeMode.CanResizeWithGrip;
                }
                else
                {
                    _lastState = WindowState;
                    _lastWidth = Width;
                    _lastHeight = Height;
                    _lastLeft = Left;
                    _lastTop = Top;
                    WindowStyle = WindowStyle.None;
                    Width = SystemParameters.PrimaryScreenWidth;
                    Height = SystemParameters.PrimaryScreenHeight;
                    Top = 0;
                    Left = 0;
                    Topmost = true;
                    WindowState = WindowState.Normal;
                    ResizeMode = ResizeMode.NoResize;
                }
            }
        }
    }
}
